const express = require("express")
const router = express.Router()
const passport = require('passport')

const Profile = require('../../models/Profile')

// $rout GET api/profiles/test
// @desc user register new account
// @access public
router.get("/test",(req,res) => {
    console.log("get req " + req)
    res.json({msg:"profile works"})
});

// $rout POST api/profiles/add
// @desc add profile data
// @access private
// router.post('/add',(req,res) => {

// })


// $rout POST api/profiles/add
// @desc add profile data
// @access private
router.post('/add',passport.authenticate('jwt',{session:false}),(req,res) => {
    const profileFields = {}

    if (req.body.type) profileFields.type = req.body.type
    if (req.body.income) profileFields.income = req.body.income
    if (req.body.outcome) profileFields.outcome = req.body.outcome
    if (req.body.describe) profileFields.describe = req.body.describe
    if (req.body.remark) profileFields.remark = req.body.remark
    if (req.body.cash) profileFields.cash = req.body.cash

    new Profile(profileFields).save().then(profile=> {
        res.json(profile)
    })

})

// $rout POST api/profiles/
// @desc findall profile data
// @access private
router.get('/',passport.authenticate('jwt',{session:false}),(req,res) => {

    Profile.find().then(profile => {
        res.json(profile)
    }).catch(err => res.status(500).json("网络忙，请稍后重试！"))

})


// $rout POST api/profiles/:id
// @desc findall profile data
// @access private
router.get('/:id',passport.authenticate('jwt',{session:false}),(req,res) => {

    console.log(req.params.id)
    Profile.findOne({_id : req.params.id}).then(profile => {
        if (!profile) return res.status(200).json("数据不存在！")
        res.json(profile)
    }).catch(err => res.status(500).json("网络忙，请稍后重试！"))

})

// $rout POST api/profiles/edit/:id
// @desc update profile data
// @access private
router.post('/edit/:id',passport.authenticate('jwt',{session:false}),(req,res) => {
    const profileFields = {} //new Profile()

    if (req.body.type) profileFields.type = req.body.type
    if (req.body.income) profileFields.income = req.body.income
    if (req.body.outcome) profileFields.outcome = req.body.outcome
    if (req.body.describe) profileFields.describe = req.body.describe
    if (req.body.remark) profileFields.remark = req.body.remark
    if (req.body.cash) profileFields.cash = req.body.cash

    // we can find first and then save new words
    //console.log(req.params.id)
    // Profile.findOne({_id : req.params.id}).then(profile => {
    //     console.log(profile)
    //     if(!profile) return res.json("服务器异常")
    //     profileFields.save().then(sprofile => res.json(sprofile))
    // })

    // also,we can use findOneAndUpdate directly
    Profile.findOneAndUpdate( 
         { _id: req.params.id},
         {$set : profileFields},
         {new : true}
    ).then(profile => res.json(profile)).catch(err => res.json(err))
})

// $rout GET api/profiles/delete/:id
// @desc findall profile data
// @access private
router.get('/delete/:id',passport.authenticate('jwt',{session:false}),(req,res) => {

    Profile.findOneAndRemove({_id: req.params.id}).then(profile => {
        profile.save().then(profile => res.json(profile.type+"删除成功！"))
    }).catch(err => {res.json("删除失败！")})



})
module.exports = router